<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Chongchong
 *
 * @author dragon.hu add @2016.2.22
 */
require_once 'BaseChannel.php';

//虫虫
class Chongchong implements BaseChannel 
{
	
	function __construct() 
	{
		//echo "construct";
	}

	//put your code here
	function android($data) 
	{
		$paymentKey="e3aeadbc78204db1ad4c063c951ae956"; //签名密钥
		$ret = $this->paymentCallBack($paymentKey, ANDROID);
		echo $ret;
	}

	function ios($data) 
	{
		$paymentKey=""; //
		$ret = $this->paymentCallBack($paymentKey, IOS);
		echo $ret;
	}

	function paymentCallBack($paymentKey, $platform)
	{
		$orderLog = new ChargeOrder();
		$orderLog->log("chongchong", $_REQUEST);
		
		$transactionNo = $_REQUEST['transactionNo'];  //虫虫支付订单号
		$partnerTransactionNo = $_REQUEST['partnerTransactionNo'];  //	商户订单号
		$statusCode = $_REQUEST['statusCode'];  //	订单状态
		$productId = $_REQUEST['productId'];  //	支付商品的Id
		$orderPrice = $_REQUEST['orderPrice'];  //	订单金额
		$packageId = $_REQUEST['packageId'];  //	游戏ID
		$sign = $_REQUEST['sign'];  //
		
		$request = $_REQUEST;
		ksort($request);
		$mysign = "";
		foreach($request as $k => $v)
		{
			if ($k == 'sign' || $v == '' || $v == null)
			{
				continue;
			}
			$mysign .= $k.'='.$v.'&';
		}
		$mysign = $mysign.$paymentKey;
		
		if($sign == md5($mysign))
		{ //验证通过进行发货
			$tmpstate = STATUS_SUCCESS;
			if ($statusCode != '0000')
			{
				$tmpstate = STATUS_FAILD;
			}
			if(false == $orderLog->recordOrder($tmpstate, $orderPrice, $partnerTransactionNo, "chongchong", $platform,$transactionNo))
			{
				$ret="0002";
				$orderLog->log("chongchong",array("order"=>$partnerTransactionNo,"money"=>$orderPrice,"serverresult"=>"faild"));
			}
			else 
			{
				//支付成功
				$ret="0000";
				$orderLog->log("chongchong",array("order"=>$partnerTransactionNo,"money"=>$orderPrice,"serverresult"=>"success"));
			}
			 
		} else {
			//"$sig验证失败"
			$orderLog->log("chongchong","order:$partnerTransactionNo sign error sign:$sign mysign:$mysign");
			$ret="0002";
		}
		return $ret;
	}
	
}